package toolkit.tianshi.compress;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import com.google.common.base.Strings;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class ThumbnailsTest {
    public List<String> picSuffix = new ArrayList<String>(){{
        add("jpg");
        add("jpeg");
        add("png");
        add("bmp");
    }};

    public boolean isPic(String path) {
        String extension = FilenameUtils.getExtension(path);
        if (!Strings.isNullOrEmpty(extension)) {
            String suffix = extension.toLowerCase();
            return picSuffix.contains(suffix);
        } else {
            return false;
        }
    }
    public String upload(InputStream inputStream, String path) {
        try {
            // 文件大小
            int available = inputStream.available();
            // 对图片进行压缩处理 根据后缀名判断是否为图片
            if (isPic(path) && available > 500 * 1024) {
                // 修改后缀
                String name = path.substring(0, path.lastIndexOf("."));
                path = name + ".jpg";
                File file = new File("D:\\img" + File.separator + path);
                if (!file.getParentFile().exists()&&!file.isDirectory()){
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                } else {
                    file.createNewFile();
                }
                double accuracy = getAccuracy(available / 1024);
                Thumbnails.of(inputStream).scale(1f).outputQuality(accuracy).toFile(file);
            } else {
                File file = new File("D:\\img" + File.separator + path);
                FileUtils.copyToFile(inputStream, file);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return path;
    }

    /**
     * 自动调节精度(经验数值)
     *
     * @param size 源图片大小
     * @return 图片压缩质量比
     */
    private static double getAccuracy(long size) {
        double accuracy;
        if (size < 900) {
            accuracy = 0.85;
        } else if (size < 2047) {
            accuracy = 0.6;
        } else if (size < 3275) {
            accuracy = 0.44;
        } else {
            accuracy = 0.4;
        }
        return accuracy;
    }

    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("C:\\Users\\A11181121050449\\Desktop\\111.jpg");
//        FileInputStream fis = new FileInputStream(file);
//        FileOutputStream stream = new FileOutputStream("D:\\img\\111.jpg");
//        ImgUtil.scale(
//                fis,
//                stream,
//                0.5f//缩放比例
//        );
        FileInputStream fis = new FileInputStream(file);
        ThumbnailsTest thumbnailsTest = new ThumbnailsTest();
        String upload = thumbnailsTest.upload(fis, "111.jpg");
        System.out.println(upload);
    }
}
